﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.DepartmentDoctor;
using WoSoft.PACS.Win.Forms.SubForms;
using WoSoft.PACS.Win.MyToolBars;
using WoSoft.PACS.Data.Doctor;

namespace WoSoft.PACS.Win.Forms
{
    /// <summary>
    /// 部门人员
    /// </summary>
    public partial class FrmDepartmentAndDoctor : FrmBase
    {
        string _departmentID = string.Empty;//部门ID
        string _oldDepartmentID = string.Empty;//部门ID
        private DepartmentDoctorData departmentDoctor = new DepartmentDoctorData();

        public FrmDepartmentAndDoctor()
        {
            InitializeComponent();
        }

        public FrmDepartmentAndDoctor(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {

            List<OperationModel> functionInfos = new List<OperationModel>();

            //申请
            var appButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "添加部门",
                Image = global::WoSoft.PACS.Win.Properties.Resources.add2,
                ClickEvent = new EventHandler(AppDepartmentClick),
            };
            functionInfos.Add(appButton);

            //编辑部门
            var editDepartmentButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "编辑部门",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_edit,
                ClickEvent = new EventHandler(EditDepartmentClick),
            };
            functionInfos.Add(editDepartmentButton);

            //删除部门
            var delDepartmentButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除部门",
                Image = global::WoSoft.PACS.Win.Properties.Resources.delete2,
                ClickEvent = new EventHandler(DeleteDepartmentClick),
            };
            functionInfos.Add(delDepartmentButton);

            //创建医护人员
            var createDoctorButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "创建医护人员",
                Image = global::WoSoft.PACS.Win.Properties.Resources.user1_new,
                ClickEvent = new EventHandler(CreateDoctroClick),
            };
            functionInfos.Add(createDoctorButton);

            //编辑医护人员
            var editDoctorButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "编辑人员",
                Image = global::WoSoft.PACS.Win.Properties.Resources.user1_information,
                ClickEvent = new EventHandler(EditDoctorButton),
            };
            functionInfos.Add(editDoctorButton);

            //删除医护人员
            var delDoctorButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除人员",
                Image = global::WoSoft.PACS.Win.Properties.Resources.user1_delete,
                ClickEvent = new EventHandler(DelDoctorButton),
            };
            functionInfos.Add(delDoctorButton);

            //刷新
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新数据",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
                ClickEvent = new EventHandler(RefreshData),
            };
            functionInfos.Add(refreshButton);
            return functionInfos;
        }

        //加载窗口
        private void FrmDepartmentAndDoctor_Load(object sender, EventArgs e)
        {
            RefreshData(null, null);
        }

        //增加部门
        private void AppDepartmentClick(object sender, EventArgs e)
        {
            FrmDepartmentInfo department = new FrmDepartmentInfo(string.Empty);
            department.ShowDialog(this);
            RefreshData(null, null);
        }

        //编辑部门
        private void EditDepartmentClick(object sender, EventArgs e)
        {
            FrmDepartmentInfo department = new FrmDepartmentInfo(_departmentID);
            var r = department.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                RefreshData(null, null);
            }
        }

        //删除部门
        private void DeleteDepartmentClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_departmentID))//没有选择部门无法删除
            {
                return;
            }
            if (this.advTree1.SelectedNode.HasChildNodes)//节点下还有子节点，不允许直接删除
            {
                MessageBoxEx.Show("该部门包括子部门，不允许删除！", "提示");
                return;
            }

            //确定要删除
            if (MessageBoxEx.Show("您确定要删除部门吗?该部门下的人员信息也将被删除。", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                departmentDoctor.DeleteDepartment(_departmentID);
                RefreshData(null, null);
            }
        }

        //创建医护人员
        private void CreateDoctroClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_departmentID))//没有选择部门无法创建医护人员
            {
                MessageBoxEx.Show("请选择一个科室后添加医生", "添加医生", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var frmdoc = new FrmDoctorInfo(_departmentID, "");
            frmdoc.ShowDialog(this);

            //刷新用户
            RefreshData(null, null);
        }

        //编辑医护人员
        private void EditDoctorButton(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_departmentID))//没有选择部门无法编辑
            {
                return;
            }
            if (this.superGridControl1.GetSelectedRows().Count == 1)
            {
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                FrmDoctorInfo frmDoctor = new FrmDoctorInfo(_departmentID, gridRow.Cells["ID"].Value.ToString());
                var r = frmDoctor.ShowDialog(this);
                if (r == System.Windows.Forms.DialogResult.OK)
                {
                    //刷新用户
                    RefreshData(null, null);
                }
            }
        }

        //删除医护人员
        private void DelDoctorButton(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_departmentID))//没有选择部门无法删除医护人员
            {
                return;
            }
            if (this.superGridControl1.GetSelectedRows().Count == 1)
            {
                if (MessageBoxEx.Show("您确认要删除选择的医护人员吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                DoctorData doctorData = new DoctorData();
                DevComponents.DotNetBar.SuperGrid.GridRow gridRow = this.superGridControl1.GetSelectedRows()[0] as DevComponents.DotNetBar.SuperGrid.GridRow;
                doctorData.Delete(gridRow.Cells["ID"].Value.ToString());
                //刷新用户
                RefreshData(null, null);
            }
            else
            {
                MessageBoxEx.Show("请选择一个需要删除的医护人员");
            }
        }

        //刷新数据
        private void RefreshData(object sender, EventArgs e)
        {
            //记录旧的ID
            _oldDepartmentID = _departmentID;

            var dt = departmentDoctor.GetAllList();

            this.advTree1.DisplayMembers = "名称";
            this.advTree1.ValueMember = "ID";
            this.advTree1.DataSource = dt;
            this.advTree1.ParentFieldNames = "ID,上级ID";
            this.advTree1.ExpandAll();

            if (_departmentID != _oldDepartmentID)
            {
                this.advTree1.SelectedValue = _oldDepartmentID;
            }
        }

        //刷新医护人员
        //private void RefreshDoctorButton(object sender, EventArgs e)
        //{
        //    var dt = departmentDoctor.GetAllList();

        //    this.advTree1.DisplayMembers = "简称";
        //    this.advTree1.ValueMember = "ID";
        //    this.advTree1.DataSource = dt;

        //    this.advTree1.ParentFieldNames = "ID,上级ID";
        //    this.advTree1.ExpandAll();
        //}

        //节点单击事件,即选择了部门
        private void advTree1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.advTree1.SelectedValue != null)
            {
                _departmentID = this.advTree1.SelectedValue.ToString();
                var dt = departmentDoctor.GetDataByDepartmentID(_departmentID);
                this.superGridControl1.PrimaryGrid.DataSource = dt;
            }
        }


    }
}
